package com.jlboot.app.admin.util;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.jfinal.kit.StrKit;
import com.jlboot.model.po.sys.SysIdGenerator;
import com.jlboot.model.po.sys.SysRole;
import com.jlboot.model.po.sys.SysUser;
import com.jlboot.core.id.UUIDHex;
import com.jlboot.core.json.Jmode;
import com.jlboot.core.util.StringUtil;
import com.jlboot.core.form.FormSelect;
import com.jlboot.model.po.sys.SysPost;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 51594 on 2022/7/28.
 */
@Slf4j
public class SysUserUtil {

    public static SysUserUtil n() {
        return new SysUserUtil();
    }

    /**
     * 取得所有未删除的帐号
     *
     * @return
     */
    public List<SysUser> getActiveUserList() {
        return SysUser.dao.find("select * from sys_user where del_tag=0 order by id");
    }

    /**
     * 取得岗位xselect数据列表
     *
     * @return
     */
    public List getSysPostList() {
        List<SysPost> postlist = SysPost.dao.find("select * from sys_post order by sort");
        List list = new ArrayList();
        for (SysPost st : postlist) {
            String post_id = st.mod.getStr("id");
            String post_name = st.mod.getStr("post_name");
            list.add(Jmode.n().set("name", post_name).set("value", post_id));
        }
        return list;
    }

    /**
     * 取得用户的所有角色列表
     *
     * @param userId
     * @return
     */
    public String getUserRoleString(Long userId) {
        List<SysRole> rolelist = SysRole.dao.find("select * from sys_user_role where user_id=?", userId);
        StringBuffer sb = new StringBuffer("|");
        for (SysRole role : rolelist) {
            sb.append(role.getStr("id")).append("|");
        }
        if (sb.indexOf("|login|") == -1) sb.append("login").append("|");
        return sb.toString();
    }

    /**
     * @param role_id
     * @return
     */
    public FormSelect getRoleUserList(String role_id) {
        FormSelect fs = FormSelect.n().setName("id");
        fs.setSource("select u.emp_id as id,u.user_name as name from sys_user u, sys_user_role r  where r.user_id=u.emp_id and r.role_id='" + role_id + "'");
        return fs;
    }

    /**
     * 给用记有配置指定角色
     *
     * @param userId
     * @param role_id
     */
    public void saveUserRole(Long userId, String role_id) {
        Db.update("delete from sys_user_role where role_id=? and user_id=?", role_id, userId);
        Db.save("sys_user_role",
                new Record().set("id", SysIdGenerator.dao.getNextId("sys_user_role"))
                .set("role_id", role_id).set("user_id", userId)
        );
    }

    /**
     * @param posts
     * @return
     */
    public static String transPost(String posts) {
        posts = StringUtil.replace(posts, ",", "|");
        if (!posts.startsWith("|")) posts = "|" + posts;
        if (!posts.endsWith("|")) posts += "|";
        return posts;
    }


    /**
     * @param posts
     * @return
     */
    public static String unTransPost(String posts) {
        posts = StringUtil.replace(posts, "|", ",");
        if (posts.startsWith(",")) posts = posts.substring(1, posts.length());
        if (posts.endsWith(",")) posts = posts.substring(0, posts.length() - 1);
        return posts;
    }

    public static String encryptUserPwd(String password,String salt){
        return SaSecureUtil.sha256(salt+"_"+password);
    }

    public static boolean checkPassword(String password,String salt,String inputPassword){

        if(StrKit.isBlank(password)||StrKit.isBlank(salt)){
            log.error("password或者salt不能为空");
            return false;
        }

        String enPassword=encryptUserPwd(password,salt);
        return enPassword.equals(inputPassword);
    }

    public static boolean resetPassword(String userId,String password){
        String saltNew=UUIDHex.getUUIDHex();
        String enPasswordNew=SysUserUtil.encryptUserPwd(password,saltNew);

        SysUser.dao.findById(userId)
                .set("user_pwd", enPasswordNew)
                .set("salt", saltNew)
                .update();
        return true;
    }

}
